﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocReleaseFromCls
    {
        public string DocReleaseFromId;
        public string DocReleaseFromName;
        public int    SortIndex=0;
        public string frkGroupDataId;
        public int TotalItem = 0;
    }

    public class DocReleaseFromParser
    {
        public static DocReleaseFromCls CreateInstance()
        {
            //tao 1 class trang....
            DocReleaseFromCls oDocReleaseFromCls = new DocReleaseFromCls();
            return oDocReleaseFromCls;
        }

        public static DocReleaseFromCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocReleaseFromCls oDocReleaseFromCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocReleaseFromCls;
        }


        public static DocReleaseFromCls ParseFromDataRow(DataRow dr)
        {
            DocReleaseFromCls oDocReleaseFromCls = new DocReleaseFromCls();
            oDocReleaseFromCls.DocReleaseFromId = XmlUtility.GetString(dr, "DocReleaseFromId", true);
            oDocReleaseFromCls.frkGroupDataId = XmlUtility.GetString(dr, "frkGroupDataId", true);
            oDocReleaseFromCls.DocReleaseFromName = XmlUtility.GetString(dr, "DocReleaseFromName", true);
            oDocReleaseFromCls.SortIndex = XmlUtility.GetInt(dr, "SortIndex", true);
            oDocReleaseFromCls.TotalItem = XmlUtility.GetInt(dr, "TotalItem", true);
            return oDocReleaseFromCls;
        }

        public static DocReleaseFromCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocReleaseFromCls[] DocReleaseFroms = new DocReleaseFromCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocReleaseFroms[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocReleaseFroms;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("DocReleaseFromId");
            ds.Tables["Info"].Columns.Add("frkGroupDataId");
            ds.Tables["Info"].Columns.Add("DocReleaseFromName");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("TotalItem");
            return ds;
        }

        public static string GetXml(DocReleaseFromCls oDocReleaseFromCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocReleaseFromCls.DocReleaseFromId,
                oDocReleaseFromCls.frkGroupDataId,
                oDocReleaseFromCls.DocReleaseFromName,
                oDocReleaseFromCls.SortIndex,
                oDocReleaseFromCls.TotalItem,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocReleaseFromCls oDocReleaseFrom = DocReleaseFromParser.ParseFromDataRow(dr);
            string Xml = DocReleaseFromParser.GetXml(oDocReleaseFrom);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocReleaseFromCls[] DocReleaseFroms = new DocReleaseFromCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocReleaseFroms[iIndex] = DocReleaseFromParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocReleaseFromParser.GetXml(DocReleaseFroms);
            return Xml;
        }


        public static string GetXml(DocReleaseFromCls[] DocReleaseFroms)
        {
            if (DocReleaseFroms == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocReleaseFroms = new DocReleaseFromCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocReleaseFroms.Length; iIndex++)
            {
                DocReleaseFromCls oDocReleaseFromCls = DocReleaseFroms[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocReleaseFromCls.DocReleaseFromId,
                    oDocReleaseFromCls.frkGroupDataId,
                    oDocReleaseFromCls.DocReleaseFromName,
                    oDocReleaseFromCls.SortIndex,
                    oDocReleaseFromCls.TotalItem,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocReleaseFromCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocReleaseFromCls[0];
            }
            DocReleaseFromCls[] DocReleaseFroms = new DocReleaseFromCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocReleaseFroms[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocReleaseFroms;
        }
    }
}
